/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.global.modelo;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.transaction.UserTransaction;
import com.isd.inventario.modelo.Deposito;
import com.isd.inventario.modelo.Sector;

/**
 *
 * @author ctrosch
 */
@Entity
@Table(name = "USR_SITIOS", schema = "dbo")
public class Sitio implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Basic(optional = false)
    @Column(name = "USR_SITIOS_CODIGO", nullable = false, length = 6)
    private String codigo;
    @Basic(optional = false)
    @Column(name = "USR_SITIOS_DESCRP", nullable = false, length = 60)
    private String descrp;
        
    //Deposito
    @JoinColumn(name = "USR_SITIOS_DEPOSI", referencedColumnName = "STTDEH_DEPOSI", nullable = true)
    @ManyToOne(optional=false)
    private Deposito deposito;

    //Sector
    @JoinColumns({
        @JoinColumn(name = "USR_SITIOS_DEPOSI", referencedColumnName = "STTDEI_DEPOSI", nullable = false, insertable=false, updatable=false),
        @JoinColumn(name = "USR_SITIOS_SECTOR", referencedColumnName = "STTDEI_SECTOR", nullable = false)
    })
    @ManyToOne
    private Sector sector;

    //Correos electrónicos para recibir notificaciones de compra en pool
    @Column(name = "USR_SITIOS_EMLPED", length = 200)
    private String emailPedidos;
    
    @Column(name = "USR_SI_DEBAJA")
    private Character debaja;
    

    public Sitio() {
    }

    public Sitio(String usrSitiosCodigo) {
        this.codigo = usrSitiosCodigo;
    }

    public Sitio(String usrSitiosCodigo, String usrSitiosDescrp) {
        this.codigo = usrSitiosCodigo;
        this.descrp = usrSitiosDescrp;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public Character getDebaja() {
        return debaja;
    }

    public void setDebaja(Character debaja) {
        this.debaja = debaja;
    }

    public String getDescrp() {
        return descrp;
    }

    public void setDescrp(String descrp) {
        this.descrp = descrp;
    }

    public Deposito getDeposito() {
        return deposito;
    }

    public void setDeposito(Deposito deposito) {
        this.deposito = deposito;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public String getEmailPedidos() {
        return emailPedidos;
    }

    public void setEmailPedidos(String emailPedidos) {
        this.emailPedidos = emailPedidos;
    }
        
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (codigo != null ? codigo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Sitio)) {
            return false;
        }
        Sitio other = (Sitio) object;
        if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "tv.global.modelo.Sitio[codigo=" + codigo + "]";
    }

    public void persist(Object object) {
        /* Add this to the deployment descriptor of this module (e.g. web.xml, ejb-jar.xml):
         * <persistence-context-ref>
         * <persistence-context-ref-name>persistence/LogicalName</persistence-context-ref-name>
         * <persistence-unit-name>logic</persistence-unit-name>
         * </persistence-context-ref>
         * <resource-ref>
         * <res-ref-name>UserTransaction</res-ref-name>
         * <res-type>javax.transaction.UserTransaction</res-type>
         * <res-auth>Container</res-auth>
         * </resource-ref> */
        try {
            Context ctx = new InitialContext();
            UserTransaction utx = (UserTransaction) ctx.lookup("java:comp/env/UserTransaction");
            utx.begin();
            EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/LogicalName");
            em.persist(object);
            utx.commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            throw new RuntimeException(e);
        }
    }

}
